/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m002.ui.views;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m002.ui.controllers.M002Controller;

/**
 *
 * @author skyr
 */
public class M002View extends AbstractView {

    public static final String CAPTION_AMKA = "ΑΜΚΑ";
    public static final String CAPTION_SEARCH = "Αναζήτηση";
    public static final String CAPTION_EMPTY_AMKA = "Το ΑΜΚΑ δεν είναι έγκυρο.";
    public static final String CAPTION_FIRST_NAME = "'Ονομα";
    public static final String CAPTION_SURNAME = "Επώνυμο";
    public static final String CAPTION_ADDRESS = "Διευθυνση";
    public static final String CAPTION_CITY = "Πόλη";
    public static final String CAPTION_TK = "ΤΚ";
    public static final String CAPTION_YEAR_OF_BIRTH = "Ημερ.Γέννησης.";
    public static final String CAPTION_PHONE = "Τηλέφωνο";
    public static final String CAPTION_INSURANCE = "Ταμείο Ασφάλισης";
    private HorizontalLayout searchLayout;
    private HorizontalLayout buttonsLayout;
    private Button searchButton;
    private TextField amkaField;
    protected M002Controller controller;
    private Form resultForm;
    private Object patientObject;
    private Panel panel;

    public M002View(AbstractModule module) {
        super(module);
    }

    public M002View(AbstractModule module, Object[] args) {
        super(module, args);
        if (args != null && args.length == 2) {
            patientObject = args[1];
        }
    }

    @Override
    public ComponentContainer getView() {
        controller = new M002Controller(module);
        initilizeComponents();
        initializeListeners();
        return viewLayout;
    }

// <editor-fold defaultstate="collapsed" desc="getters">
    public HorizontalLayout getButtonsLayout() {
        return buttonsLayout;
    }

    public M002Controller getController() {
        return controller;
    }

    public HorizontalLayout getSearchLayout() {
        return searchLayout;
    }// </editor-fold>

    protected void initilizeComponents() {
        panel = new Panel("Αναζήτηση ασθενούς");
        searchLayout = new HorizontalLayout();
        searchLayout.setSpacing(true);
        buttonsLayout = new HorizontalLayout();
        buttonsLayout.setSpacing(true);
        buttonsLayout.setVisible(false);
        searchLayout.setMargin(true);

        amkaField = new TextField(CAPTION_AMKA);
        searchButton = new Button(CAPTION_SEARCH);

        resultForm = new Form();
        resultForm.setVisible(false);
        final FormFieldFactory oldFormFieldFactory = resultForm.getFormFieldFactory();
        resultForm.setFormFieldFactory(new FormFieldFactory() {

            @Override
            public Field createField(Item item, Object propertyId, Component uiContext) {
                Field f = oldFormFieldFactory.createField(item, propertyId, uiContext);
                if (propertyId.equals("firstName")) {
                    f.setCaption(CAPTION_FIRST_NAME);
                } else if (propertyId.equals("surname")) {
                    f.setCaption(CAPTION_SURNAME);
                } else if (propertyId.equals("amka")) {
                    f.setCaption(CAPTION_AMKA);
                } else if (propertyId.equals("yearOfBirth")) {
                    f.setCaption(CAPTION_YEAR_OF_BIRTH);
                } else if (propertyId.equals("address")) {
                    f.setCaption(CAPTION_ADDRESS);
                } else if (propertyId.equals("city")) {
                    f.setCaption(CAPTION_CITY);
                } else if (propertyId.equals("tk")) {
                    f.setCaption(CAPTION_TK);
                } else if (propertyId.equals("phone")) {
                    f.setCaption(CAPTION_PHONE);
                } else if (propertyId.equals("t004Insurance")) {
                    f.setCaption(CAPTION_INSURANCE);
                }
                if (f instanceof TextField) {
                    ((TextField) f).setNullRepresentation("");
                }
                return f;
            }
        });

        if (patientObject != null) {
            amkaField.setValue(controller.getAmkaByPatientObject(patientObject));
            initializeResult();
        }

        searchLayout.addComponent(amkaField);
        searchLayout.addComponent(searchButton);
        searchLayout.setComponentAlignment(searchButton, Alignment.BOTTOM_CENTER);
        panel.addComponent(searchLayout);
        panel.addComponent(resultForm);
        panel.addComponent(buttonsLayout);
        viewLayout.addComponent(panel);
    }

    protected void initializeListeners() {
        searchButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                initializeResult();
            }
        });
    }

    protected void initializeResult() {
        if (amkaField.getValue() != null && !amkaField.getValue().toString().equals("")) {
            BeanItem patientItem = controller.getPatientDataSource(amkaField.getValue().toString());
            if (patientItem != null) {
                resultForm.setItemDataSource(patientItem);
                resultForm.setVisibleItemProperties(new String[]{
                            "firstName",
                            "surname",
                            "yearOfBirth",
                            "address",
                            "city",
                            "tk",
                            "phone",
                            "t004Insurance"
                        });
                resultForm.setVisible(true);
                resultForm.setReadOnly(true);
                buttonsLayout.setVisible(true);
                return;
            }
        }
        resultForm.setVisible(false);
        buttonsLayout.setVisible(false);
        viewLayout.getWindow().showNotification(CAPTION_EMPTY_AMKA, Notification.TYPE_ERROR_MESSAGE);
    }
}
